package com.servlet;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.service.Service;
import com.utils.PageTools;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/demo01")
public class Servlet extends BaseServlet{
    private Service s=new Service();

    /**
     * 分页或模糊查询
     * @param req
     * @param resp
     * @throws IOException
     */
    public void dimQuery(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("来了老弟");
        String currentpage = req.getParameter("currentpage");
        String pagesize = req.getParameter("pagesize");
        String input = req.getParameter("input");

        int cp;
        if (currentpage == null || "".equals(currentpage)) {
            cp = 1;
        } else {
            cp = Integer.parseInt(currentpage);
        }
        int ps;
        if (pagesize == null || "".equals(pagesize)) {
            ps = 5;
        } else {
            ps = Integer.parseInt(pagesize);
        }
        //开始判断
        List studentlist;
        PageTools pt;
        if (!"".equals(input) && input != null) {
            int sumsize = s.dim2Query(input);
            //封装分页工具类
            pt = new PageTools(cp, ps, sumsize);
            //满足条件的在去分页查询 在去调用service方法
            studentlist = s.search(input, pt);
        } else {
            int sumsize = s.AllStudentCount();
            //封装实体类
            pt = new PageTools(cp, ps, sumsize);
            //分页查询
            studentlist = s.dimQuery(pt);
        }

        //转json
        pt.setStudentlist(studentlist);
        String s = new ObjectMapper().writeValueAsString(pt);
        resp.getWriter().print(s);
        System.out.println(s);

    }
    public void delete(HttpServletRequest req, HttpServletResponse resp){
        String sid = req.getParameter("sid");
        int i=s.delete(sid);
        if (i>0){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
    }
    }

